package com.fernfx.xingtan.my.presenter;

import com.fernfx.xingtan.common.base.BaseEntity;
import com.fernfx.xingtan.common.base.BaseIRequestCallback;
import com.fernfx.xingtan.common.base.BaseView;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.my.contract.RealVerifyContract;
import com.fernfx.xingtan.my.entity.AuthenticationEntity;
import com.fernfx.xingtan.my.entity.RealVerifyEntity;
import com.fernfx.xingtan.my.model.RealVerifyModel;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.SoftInputUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import java.util.Map;

/**
 * Created by admin on 2018/8/15.
 */

public class RealVerifyPresenter<P extends RealVerifyContract.View> extends BaseIRequestCallback implements RealVerifyContract.Presenter {
    private RealVerifyContract.Model model;
    private P P;

    @Override
    public void init(BaseView view) {
        this.model = new RealVerifyModel();
        this.P = (P) view;
    }

    @Override
    public void request(Map<String, Object> requestArgsMap) {
        P.getActivity().showLoading();
        //请求接口
        model.request(requestArgsMap, this);
    }

    @Override
    public void onSuccessful(NetworkOKResult result) {
        hideLoading();
        String message = result.getMessage();

        //修改昵称成功了之后做的逻辑
        RealVerifyEntity realVerifyEntity = OtherUtil.jsonConvertEntity(message,RealVerifyEntity.class);
        if (OtherUtil.checkRequestStatus(realVerifyEntity)) {
            P.showRealVerifyContent(realVerifyEntity);
        }else {
            ToastUtil.showCentertoast(realVerifyEntity.getMsg());
        }
    }

    @Override
    public void saveAuthentication(Map<String, Object> requestArgsMap) {
        model.saveAuthentication(requestArgsMap, new BaseIRequestCallback() {
            @Override
            public void onSuccessful(NetworkOKResult result) {
                String message = result.getMessage();
                AuthenticationEntity authenticationEntity = OtherUtil.jsonConvertEntity(message, AuthenticationEntity.class);
                if (OtherUtil.checkRequestStatus(authenticationEntity)) {
                    //判断是否有软键盘如果有软键盘则关闭软键盘
                    SoftInputUtil.hideKeyboard(P.getActivity());
                    ToastUtil.showCentertoast("已提交实名认证,待审核");
                    P.getActivity().finish();
                }else {
                    ToastUtil.showCentertoast(authenticationEntity.getMsg());
                }
            }
        });
    }

    @Override
    public void detachView() {
        if (null != model) {
            model = null;
        }
    }
}
